define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'printmanager/order/index' + location.search,
                    add_url: 'printmanager/order/add',
                    edit_url: 'printmanager/order/edit',
                    del_url: 'printmanager/order/del',
                    send_url: 'printmanager/order/send',
                    done_url: 'printmanager/order/done',
                    pay_status: 'printmanager/order/pay_status',
                    close: 'printmanager/order/close',
                    multi_url: 'printmanager/order/multi',
                    table: 'self_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'order_no',
                            title: __('订单号')
                        },
                        {
                            field: 'user_id',
                            title: __('User_id'),
                            formatter: function(v,r, i){
                                return r.user_info ? r.user_info.nickname : ''
                            }
                        },
                        {
                            field: 'amount',
                            title: __('订单费用'),
                            operate: 'BETWEEN',
                            formatter: function(v, r, i){
                                var html = ''
                                html += '<p style="text-align: left;"><span class="title">订单金额：</span><span class="value text-primary">'+r.amount+'</span></p>';
                                html += '<p style="text-align: left;"><span class="title">配送费用：</span><span class="value text-warning">'+r.shipping_price+'</span></p>';
                                html += '<p style="text-align: left;"><span class="title">金币抵扣：</span><span class="value text-danger">'+r.gold_num+'(￥'+r.gold_amount+')</span></p>';
                                html += '<p style="text-align: left;"><span class="title">应付金额：</span><span class="value text-success">'+r.real_amount+'</span></p>';

                                return html;
                            }
                        },
                        {
                            field: 'real_amount',
                            title: __('应付金额'),
                            operate: 'BETWEEN',
                            visible: false
                        },
                        {
                            field: 'gold_num',
                            title: __('Gold_num'),
                            searchable: false,
                            operate: false,
                            visible: false
                        },
                        {
                            field: 'gold_amount',
                            title: __('Gold_amount'),
                            searchable: false,
                            operate: false,
                            visible: false
                        },
                        {
                            field: 'shipping_type',
                            title: __('Shipping_type'),
                            searchList: {
                                "0": __('Shipping_type 0'),
                                "1": __('Shipping_type 1')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'address_id',
                            title: __('配送地址'),
                            formatter: function(v,r, i){
                                var str = r.shipping_type  == 1 ? '自提' : (r.address ? r.address.province + r.address.city + r.address.county + r.address.address  : '')

                                return '<a style="width: 150px;word-break: break-word;white-space: normal;display: inline-block;">'+str+'</a>' 
                            }
                        },
                        {
                            field: 'shipping_price',
                            title: __('Shipping_price'),
                            searchable: false,
                            operate: false,
                            visible: false
                        },
                        {
                            field: 'remark',
                            title: __('Remark'),
                            searchable: false,
                            operate: false
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {
                                "0": __('Status 0'),
                                "1": __('Status 1'),
                                "2": __('Status 2'),
                                "3": __('Status 3'),
                                "4": __('Status 4'),
                                "5": __('Status 5'),
                            },
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: __('修改'),
                                    title: __('修改'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-pencil',
                                    url: $.fn.bootstrapTable.defaults.extend.edit_url,
                                    callback: function (data) {
                                        table.bootstrapTable('refresh', {});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 0;
                                    }
                                },
                                {
                                    name: 'send',
                                    text: __('发货'),
                                    title: __('发货'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-rocket',
                                    url: $.fn.bootstrapTable.defaults.extend.send_url,
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh', {});
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 1 && row.shipping_type == 0;
                                    }
                                },
                                {
                                    name: 'done',
                                    text: __('订单完成'),
                                    title: __('订单完成'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-check',
                                    url: $.fn.bootstrapTable.defaults.extend.done_url,
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh', {});
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return  (row.status == 3 && row.shipping_type == 0 ) || (row.status == 1 && row.shipping_type == 1 );
                                    }
                                },
                                {
                                    name: 'close',
                                    text: __('订单取消'),
                                    title: __('订单取消'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: 'fa fa-remove',
                                    url: $.fn.bootstrapTable.defaults.extend.close,
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh', {});
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return  row.status == 0;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});